﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

namespace MyNamespace
{


class PdfToImageConverter
{
    /// <summary>
    /// pdf转图片
    /// </summary>
    /// <param name="pdfPath">pdf路径</param>
    /// <param name="imagePath">输出图片路径</param>
    /// <param name="imageName">输出图片名称</param>
    /// <param name="imagePathFormat">输出图片后缀</param>
    /// <param name="imageFormat">输出图片格式</param>
    /// <param name="startPageNum">开始页码</param>
    /// <param name="endPageNum">结束页码</param>
    public static void PdfToImage(string pdfPath, string imagePath, string imageName, string imagePathFormat, System.Drawing.Imaging.ImageFormat imageFormat, int startPageNum, int endPageNum)
    {
        #region 文件夹及路径处理
        if (!System.IO.Directory.Exists(imagePath))
        {
            System.IO.Directory.CreateDirectory(imagePath);
        }
        if (!imagePath.EndsWith("\\") && !imagePath.EndsWith("/"))
        {
            imagePath = imagePath + "\\";
        }
        if (!imagePathFormat.StartsWith("."))
        {
            imagePathFormat = "." + imagePathFormat;
        }
        #endregion
        var pdf = PdfiumViewer.PdfDocument.Load(pdfPath);//读取pdf
        var pdfPage = pdf.PageCount;//pdf页码
        var pdfSize = pdf.PageSizes;
        #region 开始结束页
        if (startPageNum <= 0) { startPageNum = 1; }
        if (endPageNum > pdf.PageCount) { endPageNum = pdf.PageCount; }
        if (startPageNum > endPageNum)//开始>结束
        {
            int tempPageNum = startPageNum;
            startPageNum = endPageNum;
            endPageNum = startPageNum;
        }
        #endregion

        for (int i = startPageNum; i <= endPageNum; i++)
        {
            System.Drawing.Size size = new System.Drawing.Size();
            //pdfSize为list类型，索引从0，而pdf页码从1开始，所以需要-1
            size.Width = (int)pdfSize[i - 1].Width;
            size.Height = (int)pdfSize[i - 1].Height;
            var stream = new System.IO.FileStream($"{imagePath}{imageName}-{i}{imagePathFormat}", System.IO.FileMode.Create);
            var image = pdf.Render(i - 1, size.Width, size.Height, 300, 300, PdfiumViewer.PdfRenderFlags.Annotations);
            image.Save(stream, imageFormat);
            stream.Close();
            image.Dispose();
            stream.Dispose();
            //System.Diagnostics.Process.Start(imagePath);
        }
        pdf.Dispose();
    }
}

class Program
{
    static void Main()
    {
        string pdfFilePath = "your_pdf_file_path.pdf";
            var dir = "E:\\乐高\\乐高机器人大小颗粒ev3视频课程ppt课件教案wedo2.0学习教程资料\\乐高资料全套\\02WEDO2.0合集371节\\01WEDO2.0PDF版291节\\搭建手册";

            foreach (var item in Directory.GetFiles(dir))
            {
                var file=new FileInfo(item);
                var name = file.Name.Substring(0, file.Name.Length - 4);
                PdfToImageConverter.PdfToImage(item, $"D:\\wedo\\官方图纸\\{name}", name, ".jpg", ImageFormat.Jpeg, 1, 1000);
                Console.WriteLine(file.FullName);
            }

        
    }
}
}